<?php

namespace App\Jobs;

use App\Models\ActivitySale;
use App\Models\ExportLog;
use App\Models\Marketing;
use App\Models\UserReward;
use function foo\func;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use SuperClosure\Serializer;

class MarketingData implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, SerializesModels;

    public $queue = 'marketing';

//    public $tries = 5;

//    protected $type;

    protected $marketing;//发放
    protected $marketing_id;//发放id

    protected $activitySale;//优惠券

    protected $status;

    protected $query; //传入数据 没有则执行查询

    protected $serialize;

    protected $data;

    protected $call;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Marketing $marketing,$marketing_id, $query ,$status)
    {
        $this->marketing = $marketing;
        $this->marketing_id = $marketing_id;
        $this->data = $query;
        $this->status = $status;
    }
    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $marketing = DB::table('marketing')->where('id',$this->marketing_id)->first();
        $activitySale = DB::table('activity_sale')->where('id',$marketing->activity_sale_id)->first();
        $user_list = $this->data;
        if (!is_null($user_list[0])) {
            foreach ($user_list as $item => $value) {
                $data[$item]['user_id'] = ($marketing->type == 2 ? $value['user_id'] : $value['id']);
                $data[$item]['activity_sale_id'] =$marketing->activity_sale_id;
                $data[$item]['card_name'] = $marketing->activity_name;
                $data[$item]['type_source'] = 1;
                $data[$item]['full'] = $activitySale->full??0;
                $data[$item]['money'] = $activitySale->less??0;
                $data[$item]['time_end_valid'] = strtotime($activitySale->end_at);
                $data[$item]['time_start_valid'] = strtotime($activitySale->start_at);
                $data[$item]['created_at'] = date('Y-m-d h:m:s');
                $data[$item]['market_id'] = $this->marketing->id;
                Cache::store('redis')->forget('user:info_' . ($marketing->type == 2 ? $value['user_id'] : $value['id']));

            }
            try {
                DB::beginTransaction();
                DB::table('user_reward')->insert($data);
                DB::table('activity_sale')->where('id',$marketing->activity_sale_id)->update([
                    'amount'        => $activitySale->amount+count($user_list),
                ]);
                DB::table('marketing')->where('id',$this->marketing_id)->update([
                    'amount'        => $marketing->amount+count($user_list),
                    'status'      => Marketing::STATUS_TWO,
                ]);
                DB::commit();
            } catch (\Exception $e) {
                DB::rollBack();
            }
        }
    }
}
